"""
输入内容
时间：2024/8/22 下午1:41
"""
import warnings
warnings.filterwarnings("ignore")
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks


# 计算相似度
def calculate_similarity(text1, text2):
    similarity_pipeline = pipeline(
        task=Tasks.sentence_similarity,
        model='damo/nlp_structbert_sentence-similarity_chinese-retail-base',
        model_revision='v1.0.0'
    )
    pipeline_result = similarity_pipeline(input=(text1, text2))
    print(pipeline_result)
    similarity_index = pipeline_result['labels'].index('相似')

    similarity_percentage = pipeline_result['scores'][similarity_index] * 100
    # print(f'similarity:{similarity_percentage:.2f}%')
    return similarity_percentage


text1 = '2024年奥运会的举办地是法国巴黎。'
text2 = '法国的首都是巴黎。'
result = calculate_similarity(text1, text2)
print(f'similarity: {result:.2f}%')


